<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright © 2005-2008, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Libraries - Article Management
*  
* This library handles the article management functions of Ineo,
* including adding and removing articles, loading article data, etc.
*
* @package		Ineo
* @subpackage   Libraries
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/

class ineo_lib_articles
{
	/**
	* Ineo Class Reference
	*/
	var $core;
	
	/**
	* IPSClass Reference
	*/
	var $ipsclass;
	
	/**
	* Get Article - Loads an article and all of it's associated data, by ID.
	* 
	* @since 1.0 Beta 3
	* @see   ineo_lib_articles::load_article_data()
	* @todo  other application support
	*/
	function get_article( $article_id )
	{
		$this->_get_article_ipb_query( $article_id );

	    $article = $this->ipsclass->DB->simple_exec();
	    $article = $this->ipsclass->DB->fetch_row( $article );

		if( !is_numeric($article['article_author'] ) )
		{
			// Guest?
			$article['members_display_name'] = $article['article_author'];
			$article['article_author'] = 0;
		}

		if( !is_numeric($article['article_edited_by'] ) )
		{
			// Guest?
			$article['last_edited_by'] = $article['article_edited_by'];
			$article['article_edited_by'] = 0;
		}

		if( !is_numeric($article['content_author'] ) )
		{
			// Guest?
			$article['content_author'] = 0;
		}
		
	    return $article;
	}
	
	/**
	* Get Article Query For IPB
	* 
	* @since 1.0 Beta 3
	* @see   ineo_lib_articles::get_article()
	*/
	function _get_article_ipb_query( $id )
	{
		$this->ipsclass->DB->simple_construct( array( 'select' => 'a.*, c.*, g.g_dohtml, g.g_id, m.members_display_name, e.mgroup, e.members_display_name as last_edited_by, cat.category_name',
	                                                  'from' => array( 'ams_articles' => 'a'),
	                                                  'add_join' => array( 1 =>
	                                                                array( 'type' => 'left',
	                                                                       'from' => array( 'ams_content' => 'c'),
	                                                                       'where' => 'c.content_id=a.article_content'
	                                                                     ),
	                                                                       2 =>
	                                                                array( 'type' => 'left',
	                                                                       'from' => array( 'members' => 'm' ),
	                                                                       'where' => 'm.id=a.article_author'
	                                                                     ),
	                                                                       3 =>
	                                                                array( 'type' => 'left',
	                                                                       'from' => array( 'members' => 'e' ),
	                                                                       'where' => 'e.id=a.article_edited_by'
	                                                                     ),
	                                                                       4 =>
	                                                                array( 'type' => 'left',
	                                                                       'from' => array( 'groups' => 'g' ),
	                                                                       'where' => 'g.g_id=e.mgroup'
	                                                                     ),
	                                                                       5 =>
	                                                                array( 'type' => 'left',
	                                                                       'from' => array( 'ams_categories' => 'cat' ),
	                                                                       'where' => 'cat.category_id = a.article_cat_id'
	                                                                     )
	                                                                     ),
	                                                  'where' => 'a.article_id = ' . $id
	                                                  ) );
	}
	
	
	/**
	* Load Edit Data - Loads enough data for use by the article editor.
	*/
	function load_edit_data( $id )
	{
	  // This query pulls the article data, yeee!
	  $this->ipsclass->DB->simple_construct( array( 'select' => 'a.*, c.*',
                                                  'from' => array( 'ams_articles' => 'a'),
                                                  'add_join' => array( 1 =>
                                                                array( 'type' => 'left',
                                                                       'from' => array( 'ams_content' => 'c'),
                                                                       'where' => 'a.article_content=c.content_id'
                                                                     )
                                                                     ),
                                                  'where' => 'a.article_id = ' . $id
                                                  ) );
    $article = $this->ipsclass->DB->simple_exec();
    $article = $this->ipsclass->DB->fetch_row( $article );
    return $article;
	}
	
	/**
	* Get Attachments - Returns an array of attachments for a given article.
	*
	* @since 1.0 Beta 3
	* @see   ineo_lib_articles::load_attachment_data()
	*/
	function get_attachments( $id )
	{
		$this->ipsclass->DB->simple_construct( array( 	'select'   => 'a.*,t.*,m.members_display_name',
		                                              	'from'     => array('ams_attachments' => 'a'),
														'add_join' => array( 1 => 
		                                                              array( 'type' => 'left',
		                                                                     'from' => array( 'attachments_type' => 't' ),
		                                                                     'where' => 't.atype_extension = a.attach_ext' ),
																			2 => 
							                                          array( 'type' => 'left',
							                                                 'from' => array( 'members' => 'm' ),
							                                                 'where' => 'm.id = a.attach_mid' ),
																			),

														'where'    => 'a.attach_article = ' . $id,
													) );
			$this->ipsclass->DB->simple_order( 'attach_id ASC' );
	    $this->ipsclass->DB->simple_exec();
	    $attachments = array();
	
	    while( $data = $this->ipsclass->DB->fetch_row() )
	    {
	      $attachments[] = $data;
	    }
		
		return $attachments;
	}
	
	/**
	* Delete Article - Guess what it does!!!
	*
	* Note, there will be no warning. This function actually deletes the article.
	*
	* @since 1.0 Beta 3
	* @param int article id.
	*/
	function delete_article( $id )
	{
		$id = intval($id);
		
		// Delete Article, Article Content, Watchlist Items and Ratings.
		$this->ipsclass->DB->simple_delete( 'ams_articles'    , 'article_id = \'' . $id . '\'' );
		$this->ipsclass->DB->simple_exec();
		$this->ipsclass->DB->simple_delete( 'ams_content'     , 'content_article = \'' . $id . '\'' );
		$this->ipsclass->DB->simple_exec();
		$this->ipsclass->DB->simple_delete( 'ams_watchlists'  , 'watch_id = \'' . $id . '\' AND watch_type = 1' );
		$this->ipsclass->DB->simple_exec();
		$this->ipsclass->DB->simple_delete( 'ams_rating'      , 'rate_artid = ' . $id );
		$this->ipsclass->DB->simple_exec();
		
		// Delete article's attachments.
    	$this->ipsclass->DB->simple_construct( array( 	'select' => '*',
	                                              		'from' => 'ams_attachments',
	                                              		'where' => 'attach_article = ' . $id ) );
    	$this->ipsclass->DB->simple_exec();
    	
		while( $data = $this->ipsclass->DB->fetch_row() )
    	{
			@unlink($data['attach_location']);
    	}
    
    	// Remove attachment entries in the DB for article.
    	$this->ipsclass->DB->simple_delete( 'ams_attachments', 'attach_article = ' . $id );
    	$this->ipsclass->DB->simple_exec();
    	
		// Log Action:
		$this->core->libs['global']->add_log('delart', $id, 'Deleted article.');
		
		return true;
	}
	
	/**
	* Edit an article, create a new revision, queue it, all that fun stuff.
	* 
	* @since 1.0 Beta 3
	*/
	function full_edit( $data )
	{
	  $author      = ( isset( $data['guestname'] ) ) ? $data['guestname'] : $this->ipsclass->member['id'] ;
		$title       = strip_tags( $data['title'] );
		$description = strip_tags( $data['description'] );
		$category    = intval( $data['category'] );
		$links       = str_replace( "<br />", "\n", $data['article_links']);

		//----
		// Prevent article edits that remove the title
		//----

	    if ( empty($data['title']) )
		{
			$this->core->error( 'ams_notopic_error' );
			return false;
		}

		//----
		// Find out how many revisions there are, this is how we update the count!
		//----

		$this->ipsclass->DB->simple_select( 'article_revisions', 'ams_articles', 'article_id = ' . $data['id'] );
		$this->ipsclass->DB->simple_limit( 1 );
		$count = $this->ipsclass->DB->simple_exec();
		$count = $this->ipsclass->DB->fetch_row( $count );
		
		$content = $this->core->api->database_parse( $data['article_content'] );

	    if ( $this->ipsclass->member['g_ams_queue'] == '0' && $this->ipsclass->vars['ams_queue_edit'] == '1' )
	    {
	      $insert = array( 'content_article' => $data['id'],
	                       'content_author' => $author,
	                       'content_date' => time(),
	                       'content_content' => $content,
	                       'content_is_queued' => '1' );
	    }
	    else
	    {
	      $insert = array( 'content_article' => $data['id'],
	                       'content_author' => $author,
	                       'content_date' => time(),
	                       'content_content' => $content );
	    }

		  //----
		  // Check to see if the content has changed. If not, we're not making a new revision.
		  // We also won't be updating anything but the title and description.
		  //----

		  $this->ipsclass->DB->simple_construct( array( 'select' => 'a.*,c.*',
		                                                'from' => array( 'ams_articles' => 'a' ),
		                                                'add_join' => array( 1 => array(
		                                                              'type' => 'left',
		                                                              'from' => array( 'ams_content' => 'c' ),
		                                                              'where' => 'c.content_id = a.article_content' ) ),
		                                                'where' => 'a.article_id = ' . $data['id'] ) );
		  $this->ipsclass->DB->simple_exec();
		  $content_check = $this->ipsclass->DB->fetch_row();
		
		  if ( md5($content) != md5($content_check['content_content']) )
		  {			
	      	$msg = '1';
	      	$newcount = $count['article_revisions'] + 1;

	      	// Inserts revision
	      	$this->ipsclass->DB->do_insert( 'ams_content' , $insert );
	      	$cont_id = $this->ipsclass->DB->get_insert_id();

	      	//----
	      	// If the revision is queued, we certainly don't want to update the article table with it's id!
	      	//----

	      	/**
	      	* @todo Probably move the links into a revision, and not be static. Review with Dan.
	      	*/

	      	if ( $this->ipsclass->member['g_ams_queue'] == '0' && $this->ipsclass->vars['ams_queue_edit'] == '1' )
	      	{
	        	$update = array( 'article_title' => $title,
	                         'article_description' => $description,
	                         'article_cat_id' => $category,
	                         'article_revisions' => $newcount,
	                         'article_links' => $links );
	      	}
	      	else
	      	{
	        	$update = array( 'article_content' => $cont_id,
	                         'article_title' => $title,
	                         'article_description' => $description,
	                         'article_cat_id' => $category,
	                         'article_edited_by' => $author,
	                         'article_edited_date' => $insert['content_date'],
	                         'article_revisions' => $newcount,
	                         'article_links' => $links );
	      	}
		  }
		  else
		  {
	      	$msg = '0';

		    $update = array( 'article_title' => $title,
	                       'article_description' => $description,
	                       'article_cat_id' => $category,
	                       'article_links' => $links );
		  }

	    $this->ipsclass->DB->do_update( 'ams_articles', $update, 'article_id = \'' . $data['id'] . '\'' );

	    // If the edit is queued, then mark it as such
				
	    if ( $this->ipsclass->member['g_ams_queue'] == '0' && $this->ipsclass->vars['ams_queue_edit'] == '1' && $msg == '1' )
	    {
			$this->edit_is_queued = true;
	    }
	    else 
	    {
			$this->edit_is_queued = false;
	    }
	    
		return true;
	}
	
	//----------------------
	// OLD FUNCTIONS - DO NOT USE - ALIASES FOR BACKWARDS-COMPATABILITY ONLY!
	//----------------------
	
	
	
	
	/**
	* Load Attachment Data - Returns an array of attachments for a given article.
	*
	* OLD FUNCTION - USE GET_ATTACHMENTS()
	*
	* @see ineo_lib_articles::get_attachments()
	*/
	function load_attachment_data( $id )
	{
		trigger_error('Articles Library Function load_attachment_data is old, please use get_attachments()',E_USER_NOTICE);
		return $this->get_attachments( $id );
	}
	
	/**
	* Category Jump List
	*
	* OLD FUNCTION - USE LIB GLOBAL CATEGORY_DROPDOWN()
	*
	* @see ineo_lib_global::category_dropdown()
	*/
	function build_catlist( $id='' )
	{
		trigger_error('Articles Library Function build_catlist is old, please use lib_global::category_dropdown()',E_USER_NOTICE);
		return $this->core->libs['global']->category_dropdown( $id );
	}
	
	/**
	* Get Article - Loads an article and all of it's associated data, by ID.
	*
	* OLD FUNCTION - USE GET_ARTICLE( ID )
	* 
	* @see   ineo_lib_articles::get_article()
	* @todo  other application support
	*/
	function load_article_data( $article_id )
	{
		trigger_error('Articles Library Function load_article_data is old, please use get_article()',E_USER_NOTICE);
		return $this->get_article( $article_id );
	}
}

?>